package com.dongdongshop.controller;

import com.dongdongshop.data.Page;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.service.TbSpecificationService;
import com.dongdongshop.service.TbTypeTemplateService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("tbTypeTemplate")
public class TbTypeTemplateController {

    @DubboReference
    private TbTypeTemplateService tbTypeTemplateService;
    @DubboReference
    private BrandService brandService;
    @DubboReference
    private TbSpecificationService tbSpecificationService;

    @RequestMapping("toTbTypeTemplate")
    private String toTbTypeTemplate(Model model) {
        List<TbBrand> tbBrands = brandService.brandList();
        List<TbSpecification> tbSpecifications = tbSpecificationService.selectList();
        model.addAttribute("tbBrands", tbBrands);
        model.addAttribute("tbSpecifications", tbSpecifications);
        return "/admin/type_template";
    }

    @RequestMapping("tbTypeTemplateshow")
    @ResponseBody
    public Result tbTypeTemplateshow(TbTypeTemplate tbTypeTemplate, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize) {
        Page<TbTypeTemplate> tbTypeTemplatePage = tbTypeTemplateService.selectList(tbTypeTemplate, pageNum, pageSize);
        if (tbTypeTemplatePage != null) {
            return Result.response(ResultEnum.SUCCESS).setData(tbTypeTemplatePage);
        } else {
            return Result.response(ResultEnum.ERROR);

        }
    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(TbTypeTemplate tbTypeTemplate) {
        boolean b = tbTypeTemplateService.insertByT(tbTypeTemplate);
        if (b) {
            return Result.response(ResultEnum.SUCCESS);
        } else {
            return Result.response(ResultEnum.ERROR);
        }
    }

    @RequestMapping("toupdate")
    @ResponseBody
    public Result toUpdate(Long id){
        TbTypeTemplate tbTypeTemplate = tbTypeTemplateService.selectById(id);
        if (tbTypeTemplate != null) {
            return Result.response(ResultEnum.SUCCESS).setData(tbTypeTemplate);
        } else {
            return Result.response(ResultEnum.ERROR);
        }
    }


    @RequestMapping("update")
    @ResponseBody
    public Result update(TbTypeTemplate tbTypeTemplate) {
        boolean b = tbTypeTemplateService.updateByT(tbTypeTemplate);
        if (b) {
            return Result.response(ResultEnum.SUCCESS);
        } else {
            return Result.response(ResultEnum.ERROR);
        }
    }

    @RequestMapping("deleteIds")
    @ResponseBody
    public Result deleteIds(Integer[] ids){
        boolean b = tbTypeTemplateService.deleteByIds(ids);
        if (b) {
            return Result.response(ResultEnum.SUCCESS);
        } else {
            return Result.response(ResultEnum.ERROR);
        }
    }
}
